package com.qianying.controller.admin;

import com.qianying.comment.CommonResult;
import com.qianying.entity.RepairmanInfo;
import com.qianying.service.RepairmanInfoService;
import com.qianying.utils.HttpServletRequestUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 维修师傅信息Controller
 * 作者：check
 */
@RestController
@RequestMapping("/admin/repairman")
@Tag(name = "维修师傅信息管理", description = "维修师傅信息的查询、新增和更新接口")
public class RepairmanInfoController {

    @Autowired
    private RepairmanInfoService repairmanInfoService;

    /**
     * 根据ID查询维修师傅详情
     */
    @GetMapping("/getById")
    @Operation(
            summary = "根据ID查询维修师傅详情",
            description = "通过主键ID获取维修师傅的完整信息",
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "维修师傅信息Id",
                            required = true,
                            example = "1"
                    )
            }
    )
    public CommonResult<RepairmanInfo> getById(@RequestParam Long id) {
        return repairmanInfoService.getById(id);
    }

    /**
     * 新增维修师傅信息
     */
    @PostMapping("/save")
    @Operation(
            summary = "新增维修师傅信息",
            description = "创建新的维修师傅记录，包含基本信息",
            parameters = {
                    @Parameter(
                            name = "realName",
                            description = "维修师傅真实姓名，不能为空",
                            required = true,
                            example = "张三"
                    ),
                    @Parameter(
                            name = "idCard",
                            description = "维修师傅身份证号码，唯一且不能为空",
                            required = true,
                            example = "110101199001011234"
                    ),
                    @Parameter(
                            name = "idCardFront",
                            description = "身份证正面照片",
                            required = true,
                            example = "http://www.aaa/Front.png"
                    ),
                    @Parameter(
                            name = "idCardBack",
                            description = "身份证背面照片",
                            required = true,
                            example = "http://www.aaa/Back.png"
                    ),
                    @Parameter(
                            name = "address",
                            description = "维修师傅常住地址，可选填",
                            required = false,
                            example = "北京市朝阳区建国路88号"
                    ),
                    @Parameter(
                            name = "provinceId",
                            description = "省份ID，可选填",
                            required = false,
                            example = "7"
                    ),
                    @Parameter(
                            name = "cityId",
                            description = "城市ID，可选填",
                            required = false,
                            example = "8"
                    ),
                    @Parameter(
                            name = "districtId",
                            description = "区/县ID，可选填",
                            required = false,
                            example = "16"
                    )
            }
    )

    public CommonResult<?> saveRepairman(@RequestBody RepairmanInfo infor,
                                         HttpServletRequest request) {
        Long userId = HttpServletRequestUtils.GetHttpServletRequestUserId(request);
        return repairmanInfoService.saveRepairman(infor, userId);
    }

    /**
     * 更新维修师傅信息
     */
    @PostMapping("/update")
    @Operation(
            summary = "更新维修师傅信息",
            description = "修改已有维修师傅记录，仅更新传入的字段",
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "维修师傅主键ID，必须提供",
                            required = true,
                            example = "1"
                    ),
                    @Parameter(
                            name = "realName",
                            description = "维修师傅真实姓名，可选填",
                            required = false,
                            example = "张三"
                    ),
                    @Parameter(
                            name = "idCard",
                            description = "维修师傅身份证号码，可选填且需唯一",
                            required = false,
                            example = "110101199001011234"
                    ),
                    @Parameter(
                            name = "idCardFront",
                            description = "身份证正面照片",
                            required = true,
                            example = "http://www.aaa/Front.png"
                    ),
                    @Parameter(
                            name = "idCardBack",
                            description = "身份证背面照片",
                            required = true,
                            example = "http://www.aaa/Back.png"
                    ),
                    @Parameter(
                            name = "address",
                            description = "维修师傅常住地址，可选填",
                            required = false,
                            example = "北京市朝阳区建国路88号"
                    ),
                    @Parameter(
                            name = "provinceId",
                            description = "省份ID，可选填",
                            required = false,
                            example = "8"
                    ),
                    @Parameter(
                            name = "cityId",
                            description = "城市ID，可选填",
                            required = false,
                            example = "16"
                    ),
                    @Parameter(
                            name = "districtId",
                            description = "区/县ID，可选填",
                            required = false,
                            example = "17"
                    )
            }
    )
    public CommonResult<?> updateRepairman(@RequestBody RepairmanInfo infor) {
        return repairmanInfoService.updateRepairman(infor);
    }

    @GetMapping("/page")
    @Operation(
            summary = "分页查询维修师傅信息",
            description = "分页获取维修师傅信息列表，支持多条件筛选，返回整合后的DTO数据",
            parameters = {
                    @Parameter(name = "pageNum", description = "页码，默认1", example = "1", required = false),
                    @Parameter(name = "pageSize", description = "每页条数，默认10", example = "10", required = false),
                    @Parameter(name = "realName", description = "真实姓名（模糊查询）", example = "张", required = false),
                    @Parameter(name = "phone", description = "联系电话（模糊查询）", example = "138", required = false),
                    @Parameter(name = "enableStatus", description = "启用状态（0-禁用，1-启用）", example = "1", required = false),
                    @Parameter(name = "provinceId", description = "省份ID", example = "7", required = false),
                    @Parameter(name = "cityId", description = "城市ID", example = "8", required = false),
                    @Parameter(name = "districtId", description = "区/县ID", example = "16", required = false),
                    @Parameter(name = "workTypeId", description = "服务项目ID", example = "3", required = false)
            }
    )
    public CommonResult<?> getRepairmanPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String realName,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) Integer enableStatus,
            @RequestParam(required = false) Long provinceId,
            @RequestParam(required = false) Long cityId,
            @RequestParam(required = false) Long districtId,
            @RequestParam(required = false) Long workTypeId) {

        // 调用服务层分页查询方法，获取IPage<RepairmanInfoDTO>结果
        return repairmanInfoService.getRepairmanPage(pageNum, pageSize, realName, phone, enableStatus, provinceId, cityId, districtId, workTypeId);
    }
}
